<?php

/**
 * 四方平台请求接口
 * @author: junshi
 * @date: 2020-12-8 14:11:41
 */
class synchroController extends baseController
{

    private $hostDomain = "https://zct.cdsile.cn";     //域名

    /**
     * 同步政策库数据
     * @author: junshi
     * @date: 2020-12-8 14:05:25
     */
    public function getZhengcekuAction(){
        ini_set('max_execution_time', '0');
        $zhengcekuModule = new zhengcekuModule();
        $sort_id = $this->common->Get("sort_id");
        $where = [
            "status" => 1,          //未删除
            "synchro_status" => 1   //未同步
        ];
        if (!empty($sort_id) && in_array($sort_id,[4,5,6])){
            $where["sort_id"] = $sort_id;
        }
        $field = "id,sort_id,biaoti,fengmiantu,laiyuan,paixu,url,description,content,biaoqian,fawenriqi,chaifenzhuangtai,
                  youxiaozhuangtai,jiezhiriqi,jibie,bumen,fuchifangxiang,renqi,hexinneirong,fuchitiaojian,
                  shenbaocailiao,liuchengshenbao,lianxifangshi,zuigaojine,fawenzihao,fawenbumen,
                  shenbaoduixiang,zhengcemingcheng,suoshudiqu,tuijian,guanjianci,fujian,dtime";
        //查询数据
        $data = $zhengcekuModule
            ->where($where)
            ->find($field)
            ->limit(0,50)
            ->all();
        //处理数据
        $parameterModel = new parameterModel();
        $guanjianciModel = new Model("guanjianci");
        $fujianModel = new Model("fujian");
        foreach ($data as &$v){
            //缩略图
            $v["fengmiantu"] = $this->fileRoot($v["fengmiantu"]);
            //级别
            $jibie_info = $parameterModel->where("(classid = 369 or classid = 373) and id != 373 and u2 = '{$v["jibie"]}'")->one();
            $v["jibie"] = !empty($jibie_info) ? $jibie_info["u1"] : "";
            //部门
            $bumen_info = $parameterModel->where("classid = 374 and u2 = '{$v["bumen"]}'")->one();
            $v["bumen"] = !empty($bumen_info) ? $bumen_info["u1"] : "";
            if (!empty($v["fuchifangxiang"])){
                //扶持方向
                $fc = explode(",",$v["fuchifangxiang"]);
                $fc_arr = [];
                foreach ($fc as $v1){
                    $info = $parameterModel->where("classid = 376 and u2 = '$v1'")->one();
                    if (!empty($info)){
                        $fc_arr[] = $info["u1"];
                    }
                }
                $v["fuchifangxiang"] = $fc_arr;
            }
            //关键词
            $gjc = $guanjianciModel->find("ci,quanzhong,leixing")->where(["laiyuanbianhao"=>$v["guanjianci"]])->all();
            $v["guanjianci"] = !empty($gjc) ? $gjc : [];
            //附件
            $fj = $fujianModel->find("wenjian,zhuangtai,leixing,mingcheng")->where(["laiyuanbianhao"=>$v["fujian"]])->all();
            foreach ($fj as &$v2){
                $v2["wenjian"] = $this->fileRoot($v2["wenjian"]);
            }
            $v["fujian"] = !empty($fj) ? $fj : [];
            //将附件
            if (!empty($v["fujian"])){
                $str_start = "<p>";
                $str_end = "</p></br>";
                $str_content = "";
                foreach ($v["fujian"] as $vv){
                    $str_content .= "<a href='{$vv['wenjian']}' target='_blank'>{$vv['mingcheng']}</a>";
                }
                $str = $str_start.$str_content.$str_end;
                $v["content"] .= $str;
            }
        }
        returnSuccess($data);
    }

    /**
     * 同步政策库数据回调（修改同步状态）
     * @author: junshi
     * @date: 2020-12-8 16:41:04
     */
    public function getZhengcekuCallbackAction(){
        $request = Request::unmeaning()->all("ids");
        $ids = $request["ids"];
        if (empty($ids)){
            returnFail([],"ids is null");
        }
        $update = [
            "synchro_status" => 2
        ];
        $zhengcekuModule = new zhengcekuModule();
        $res = $zhengcekuModule->where("id in ($ids)")->update($update);
        if ($res){
            returnSuccess([],"成功");
        }
        returnFail([],"失败");
    }

    /**
     * 同步政策动态
     * @author: junshi
     * @date: 2020-12-8 16:57:03
     */
    public function getZhengcedongtaiAction(){
        ini_set('max_execution_time', '0');
        $zhengcedongtaiModule = new zhengcedongtaiModule();
        $where = [
            "status" => 1,          //未删除
            "synchro_status" => 1   //未同步
        ];
        $field = "id,biaoti,fengmian,laiyuan,neirong,jibie,renqi,paixu,biaoqian,fawenriqi,zhuangtai,pailiefangshi,tuijian";
        //查询数据
        $data = $zhengcedongtaiModule
            ->where($where)
            ->find($field)
            ->limit(0,50)
            ->all();
        //处理数据
        $parameterModel = new parameterModel();
        foreach ($data as &$v){
            $v["fengmian"] = $this->fileRoot($v["fengmian"]);
            //级别
            $jibie_info = $parameterModel->where("(classid = 369 or classid = 373) and id != 373 and u2 = '{$v["jibie"]}'")->one();
            $v["jibie"] = !empty($jibie_info) ? $jibie_info["u1"] : "";
        }
        returnSuccess($data);
    }

    /**
     * 同步政策动态回调（修改同步状态）
     * @author: junshi
     * @date: 2020-12-8 16:57:03
     */
    public function getZhengcedongtaiCallbackAction(){
        $request = Request::unmeaning()->all("ids");
        $ids = $request["ids"];
        if (empty($ids)){
            returnFail([],"ids is null");
        }
        $update = [
            "synchro_status" => 2
        ];
        $zhengcejieduModule = new zhengcejieduModule();
        $res = $zhengcejieduModule->where("id in ($ids)")->update($update);
        if ($res){
            returnSuccess([],"成功");
        }
        returnFail([],"失败");
    }

    /**
     * 同步政策解读
     * @author: junshi
     * @date: 2020-12-8 17:26:13
     */
    public function getZhengcejieduAction(){
        ini_set('max_execution_time', '0');
        $zhengcejieduModule = new zhengcejieduModule();
        $where = [
            "status" => 1,          //未删除
            "synchro_status" => 1   //未同步
        ];
        $field = "id,biaoti,laiyuan,paixu,url,description,content,fawenriqi,jibie,bumen,fuchifangxiang,renqi,biaoqian,zhuangtai,tuijian,dtime";
        //查询数据
        $data = $zhengcejieduModule
            ->where($where)
            ->find($field)
            ->limit(0,50)
            ->all();
        //处理数据
        $parameterModel = new parameterModel();
        foreach ($data as &$v){
            //级别
            $jibie_info = $parameterModel->where("(classid = 369 or classid = 373) and id != 373 and u2 = '{$v["jibie"]}'")->one();
            $v["jibie"] = !empty($jibie_info) ? $jibie_info["u1"] : "";
            //部门
            $bumen_info = $parameterModel->where("classid = 374 and u2 = '{$v["bumen"]}'")->one();
            $v["bumen"] = !empty($bumen_info) ? $bumen_info["u1"] : "";
            if (!empty($v["fuchifangxiang"])){
                //扶持方向
                $fc = explode(",",$v["fuchifangxiang"]);
                $fc_arr = [];
                foreach ($fc as $vv){
                    $info = $parameterModel->where("classid = 376 and u2 = '$vv'")->one();
                    if (!empty($info)){
                        $fc_arr[] = $info["u1"];
                    }
                }
                $v["fuchifangxiang"] = $fc_arr;
            }
        }
        returnSuccess($data);
    }

    /**
     * 同步政策解读回调（修改同步状态）
     * @author: junshi
     * @date: 2020-12-8 16:57:03
     */
    public function getZhengcejieduCallbackAction(){
        $request = Request::unmeaning()->all("ids");
        $ids = $request["ids"];
        if (empty($ids)){
            returnFail([],"ids is null");
        }
        $update = [
            "synchro_status" => 2
        ];
        $zhengcejieduModule = new zhengcejieduModule();
        $res = $zhengcejieduModule->where("id in ($ids)")->update($update);
        if ($res){
            returnSuccess([],"成功");
        }
        returnFail([],"失败");
    }

    //完善图片路径
    public function fileRoot($url){
        $root = !empty($url) ? $this->hostDomain.$url : "";
        return $root;
    }

}